﻿using System;
using System.ComponentModel;
using System.Windows.Input;
using GC_VKMD.MusicDownloaderClient.Helpers;
using GC_VKMD.MusicDownloaderClient.ViewModels.Parameters;

namespace GC_VKMD.MusicDownloaderClient.ViewModels
{
	public class MusicDownloaderSettingsViewModel : INotifyPropertyChanged
	{
		#region Variables

		private RelayCommand _runCommand;
		private RelayCommand _browseCommand;

		private readonly MusicDownloaderSettingsViewModelParameters _parameters;

		private bool _isAllMusicTracksChecked;
		private string _folder;
		private int _audioTrackCount;
		private string _appId;

		#endregion

		#region Properties

		public string Folder
		{
			get { return _folder; }
			set
			{
				_folder = value;
				if (PropertyChanged == null) return;
				PropertyChanged(this, new PropertyChangedEventArgs("Folder"));
			}
		}

		public int AudioTrackCount
		{
			get { return _audioTrackCount; }
			set
			{
				_audioTrackCount = value;
				if (PropertyChanged == null) return;
				PropertyChanged(this, new PropertyChangedEventArgs("AudioTrackCount"));
			}
		}

		public bool IsAllMusicTracksChecked
		{
			get { return _isAllMusicTracksChecked; }
			set
			{
				_isAllMusicTracksChecked = value;
				if (PropertyChanged == null) return;
				PropertyChanged(this, new PropertyChangedEventArgs("IsAllMusicTracksChecked"));
			}
		}

		public string AppId
		{
			get { return _appId; }
			set
			{
				_appId = value;
				if (PropertyChanged == null) return;
				PropertyChanged(this, new PropertyChangedEventArgs("AppId"));
			}
		}

		public ICommand RunButtonCommand
		{
			get { return _runCommand; }
		}

		public ICommand BrowseButtonCommand
		{
			get { return _browseCommand; }
		}

		#endregion

		#region Constructors

		public MusicDownloaderSettingsViewModel(MusicDownloaderSettingsViewModelParameters parameters)
		{
			if (parameters == null)
				throw new ArgumentNullException(string.Format("Parameters"));
			_parameters = parameters;

			if (parameters.WindowManager == null)
				throw new ArgumentNullException(string.Format("windowManager"));

			if (parameters.BrowserDialog == null)
				throw new ArgumentNullException(string.Format("BrowserDialog"));

			if (parameters.ApplicationConfiguration == null)
				throw new ArgumentNullException(string.Format("ApplicationConfiguration"));

			FillViewModel();
		}

		#endregion

		#region Internal Implementation

		private void FillViewModel()
		{
			AppId = _parameters.ApplicationConfiguration.AppId;
			Folder = _parameters.ApplicationConfiguration.Folder;
			AudioTrackCount = _parameters.ApplicationConfiguration.AudioTracksCount;

			_runCommand = new RelayCommand(Run);
			_browseCommand = new RelayCommand(Browse);
		}

		private void Run(object parameter)
		{
			_parameters.ApplicationConfiguration.AppId = AppId;
			_parameters.ApplicationConfiguration.Folder = Folder;
			_parameters.ApplicationConfiguration.AudioTracksCount = IsAllMusicTracksChecked ? 0 : AudioTrackCount;
			_parameters.ApplicationConfiguration.Token = _parameters.ApplicationConfiguration.UserId = null;

			_parameters.WindowManager.ShowVKAutorizationWindow();
		}

		private void Browse(object parameter)
		{
			Folder = _parameters.BrowserDialog.FolderBrowserDialog();
		}

		#endregion

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		protected virtual void OnPropertyChanged(string propertyName)
		{
			var handler = PropertyChanged;
			if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
		}

		#endregion
	}
}
